#include <bits/stdc++.h>
using namespace std;
set<pair<string, int>> se;
int ans = 0;
const int l1 = 25200, r1 = 32400, l2 = 39600, r2 = 46800, l3 = 79200, r3 = 90000;
void check(pair<string, string> p) {
    int pos = -1;
    string u = p.first, b = p.second;
    int x = stoi(b.substr(0, 2));
    int y = stoi(b.substr(3, 2));
    int z = stoi(b.substr(6));
    int t = x * 3600 + y * 60 + z;
    if(t <= 3600) {
        t += 86400;
    }
    int pos = -1;
    if(t >= l1 && t <= r1 || t >= 64800 && t <= 72000) {
        pos = 1;
    }else if(t >= l2 && t <= r2) {
        pos = 2;
    }else if(t >= l3 && t <= r3){
        pos = 3;
    }
    if(pos == -1) return;
    if(!se.count({u, pos})) {
        se.insert({u, pos});
        ans++;
    }
    
}

void solve() {
    int n; cin >> n;
    string h, m;
    cin >> h >> m;
    vector<pair<string, string>> vec;
    for(int i = 0; i < n; i++) {
        string u, a, b;
        cin >> u >> a >> b;
        if(a.substr(0, 4) == h && a.substr(5, 2) == m) {
            vec.push_back({u, b});
        }
    }
    for(auto p : vec) {
        check(p);
    }
    cout << ans << endl;
}

int main() {
    solve();
    return 0;
}